﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameSDK.Board;
using GameSDK.Player;
using GuessNumberGame.Player;
using GameSDK.Events;

namespace GuessNumberGame.Board
{
    public class GuessNumberBoard : StdBoard
    {
        public new event PlayerMovedEventHandler PlayerMove;

        #region Attributes

        private int _findNumber;

        private int _lowerBound;
        private int _upperBound;

        public int LowerBound
        {
            get { return _lowerBound; }
            set { _lowerBound = value; }
        }

        public int UpperBound
        {
            get { return _upperBound; }
            set { _upperBound = value; }
        }

        private Random _alea;
        public Random Alea
        {
            get { return _alea; }
            set { _alea = value; }
        }

        #endregion

        #region Constructor

        public GuessNumberBoard()
        {
        }

        public GuessNumberBoard(int lower, int upper) : this()
        {
            if (lower < upper)
            {
                LowerBound = lower;
                UpperBound = upper;
            }
            else
            {
                LowerBound = upper;
                UpperBound = lower;
            }
            Alea = new Random(DateTime.Now.Millisecond);
            _findNumber = Alea.Next(lower, upper);
        }

        #endregion


        #region Methods

        public int EvaluateMove(GuessNumberMove _move)
        {
            return (_move.Proposal - _findNumber);
        }


        public void startGame()
        {
            if (ListPlayer == null)
            {
                Console.WriteLine("We need player!!");
            }
            else if (ListPlayer.Count != 2)
            {
                Console.WriteLine("We need 2 players !!");
            }
            else
            {
                Console.WriteLine("Find the guess value between " + this.LowerBound + " and " + this.UpperBound);
                ListPlayer[0].NextMove();
            }
        }

        public void updateBound(GuessNumberMove move)
        {
            foreach (GuessNumberPlayer player in ListPlayer)
            {
                if (EvaluateMove(move) < 0)
                {
                    if (player.LowerBound < move.Proposal) player.LowerBound = move.Proposal;
                }
                else
                {
                    if (player.UpperBound > move.Proposal) player.UpperBound = move.Proposal;
                }
            }
        }

        #endregion


        #region Override


        public override void PlayerMoved(StdPlayer player, StdMove move)
        {
            if (PlayerMove != null)
            {
                updateBound((GuessNumberMove)move);
                PlayerMove(this, new MoveEventArgs(this, player, move));
            }
        }

        #endregion
    }
}
